$import('SFBusUtil');
$import('ArrayUtil');
var ViewUtil = {
	splitChar : "__"
}
var View = $createClass('View',function(param){
	
	if(!param) param = {};
	if(param.model){
		this.model = param.model;
	
	}
	if(param.name){
		this.name = param.name;
	}
	if(param.doSetCurrent)
		this.doSetCurrent = param.doSetCurrent;
	if(param.doCancelCurrent)
		this.doCancelCurrent = param.doCancelCurrent;
	
	if(this.model)
		this.bindModelMsg();
		
	this.stateSetFuns = {};
	this.cancelSetFuns = {};
	this.initState();
	
});


View.prototype.initState = function(){
	var self = this;
	this.addSetStateFun(ModelUtil.S_Current,function(param){
		self.setCurrent(param);
	})
	this.addCancelStateFun(ModelUtil.S_Current,function(param){
		self.cancelCurrent(param);
	})
}

View.prototype.bindModelMsg = function(){
	var self = this;
	var msgName = this.model.getMsgName();
	SFBusUtil.bindCtrl(msgName,ModelUtil.S_refreshMsg,function(param){
		self.refresh(param);
	});
	SFBusUtil.bindCtrl(msgName,ModelUtil.S_stateSet,function(param){
		self.setStates(param);
	});
	SFBusUtil.bindCtrl(msgName,ModelUtil.S_stateCancel,function(param){
		self.cancelStates(param);
	})
	
}
View.prototype.setStates = function(options){
	var stateId = options.stateId;
	if(stateId){
		ArrayUtil.foreach(this.getStateSetList(stateId),function(n,fun){
			fun(options.pojo);
		});
	}
}



View.prototype.cancelStates = function(options){
	var stateId = options.stateId;
	if(stateId){
		ArrayUtil.foreach(this.getStateCancelList(stateId),function(n,fun){
			fun(options.pojo);
		});
	}
}

View.prototype.getStateSetList = function(stateId){
	var ret = this.stateSetFuns[stateId];
	if(!ret){
		ret=[];
		this.stateSetFuns[stateId] = ret;
	}
	return ret;
		
}

View.prototype.getStateCancelList = function(stateId){
	var ret = this.cancelSetFuns[stateId];
	if(!ret){
		ret=[];
		this.cancelSetFuns[stateId] = ret;
	}
	return ret;
}

View.prototype.addSetStateFun = function(stateId,fun){
	this.getStateSetList(stateId).push(fun);
}

View.prototype.addCancelStateFun = function(stateId,fun){
	this.getStateCancelList(stateId).push(fun);
}

View.prototype.setCurrent =function(pojo){
	if(this.doSetCurrent && pojo){
		this.doSetCurrent(pojo);
	}
}

View.prototype.cancelCurrent =function(pojo){
	if(this.doCancelCurrent && pojo){
		this.doCancelCurrent(pojo);
	}
}